package dispatcher.gui.event;

import state.group.events.EventCollection;


public class StatusUpdateThreadPool{
	
	public static StatusUpdateThreadPool instance = new StatusUpdateThreadPool();
	public static StatusUpdateThreadPool getInstance(){
		return instance;
	}
	
	public static final int MAX_THREADS = 8;
	
	private StatusUpdateThread []threadPool;
	private int curThreads;
	
	public StatusUpdateThreadPool(){
		threadPool = new StatusUpdateThread[MAX_THREADS];
		curThreads = 0;
		createThreads();
	}
	
	private void createThreads(){
		for(int i = 0; i < MAX_THREADS; i++){
			threadPool[i] = new StatusUpdateThread();
			threadPool[i].start();
		}
	}
	
	public synchronized void updateStatus(EventCollection events){
		threadPool[curThreads % MAX_THREADS].setEvents(events);
		synchronized(threadPool[curThreads % MAX_THREADS]){
			threadPool[curThreads % MAX_THREADS].notifyAll();
		}
		curThreads++;
	}
	
	public synchronized void terminate(){
		for(int i = 0; i < MAX_THREADS; i++){
			threadPool[i].terminate();
			synchronized(threadPool[i]){
				threadPool[i].notifyAll();
			}
		}
	}
}
